﻿using System;
using System.Collections.Generic;
using System.IO;
using System.Net;
using System.Web;
using System.Drawing;
using System.Security.Cryptography;
using System.Text;
using System.Text.RegularExpressions;

namespace TV.Common
{
    public class Utils
    {
        public static string DownloadImageFromUrl(string imageUrl, string filename)
        {

            Image image = null;

            try
            {
                System.Net.HttpWebRequest webRequest = (System.Net.HttpWebRequest)System.Net.HttpWebRequest.Create(imageUrl);
                webRequest.AllowWriteStreamBuffering = true;
                webRequest.Timeout = 30000;

                System.Net.WebResponse webResponse = webRequest.GetResponse();

                System.IO.Stream stream = webResponse.GetResponseStream();

                image = Image.FromStream(stream);

                webResponse.Close();
                image.Save(HttpContext.Current.Server.MapPath("/images/" + filename));
                return filename;
            }
            catch (Exception ex)
            {

            }
            return "";
        }
        public static String GetTitle(string chuoi)
        {
            string[] VietnameseSigns = new string[]
         {
          "aAeEoOuUiIdDyY",
          "áàạảãâấầậẩẫăắằặẳẵ",
          "ÁÀẠẢÃÂẤẦẬẨẪĂẮẰẶẲẴ",
          "éèẹẻẽêếềệểễ",
          "ÉÈẸẺẼÊẾỀỆỂỄ",
          "óòọỏõôốồộổỗơớờợởỡ",
          "ÓÒỌỎÕÔỐỒỘỔỖƠỚỜỢỞỠ",
          "úùụủũưứừựửữ",
          "ÚÙỤỦŨƯỨỪỰỬỮ",
          "íìịỉĩ",
          "ÍÌỊỈĨ",
          "đ",
          "Đ",
          "ýỳỵỷỹ",
          "ÝỲỴỶỸ"
         };
            string str = chuoi.ToLower();
            for (int i = 1; i < VietnameseSigns.Length; i++)
            {
                for (int j = 0; j < VietnameseSigns[i].Length; j++)
                    str = str.Replace(VietnameseSigns[i][j], VietnameseSigns[0][i - 1]);
            }
            String a = str;
            a = a.Replace(" ", "-").Replace("?", "").Replace(".", "").Replace("&", "").Replace("''", "").Replace(",", "").Replace("\"", "").Replace("/", "-").Replace("--", "-").Replace("(", "-").Replace(")", "-");
            a = Regex.Replace(a, "--", "-");
            return a;

        }
        public static string RemoveSign4VietnameseString(string str)
        {

            //Tiến hành thay thế , lọc bỏ dấu cho chuỗi
            for (int i = 1; i < VietnameseSigns.Length; i++)
            {
                for (int j = 0; j < VietnameseSigns[i].Length; j++)
                    str = str.Replace(VietnameseSigns[i][j], VietnameseSigns[0][i - 1]);
            }
            return str;
        }
        private static readonly string[] VietnameseSigns = new string[]
     {
      "aAeEoOuUiIdDyY",
      "áàạảãâấầậẩẫăắằặẳẵ",
      "ÁÀẠẢÃÂẤẦẬẨẪĂẮẰẶẲẴ",
      "éèẹẻẽêếềệểễ",
      "ÉÈẸẺẼÊẾỀỆỂỄ",
      "óòọỏõôốồộổỗơớờợởỡ",
      "ÓÒỌỎÕÔỐỒỘỔỖƠỚỜỢỞỠ",
      "úùụủũưứừựửữ",
      "ÚÙỤỦŨƯỨỪỰỬỮ",
      "íìịỉĩ",
      "ÍÌỊỈĨ",
      "đ",
      "Đ",
      "ýỳỵỷỹ",
      "ÝỲỴỶỸ"
     };

        public static int getIntQuery(string id)
        {
            int number;
            if (HttpContext.Current.Request.QueryString[id] != null)
            {
                if ((!Int32.TryParse(HttpContext.Current.Request.QueryString[id].ToString(), out number)))
                {
                    return 0;
                }
                return int.Parse(HttpContext.Current.Request.QueryString[id].ToString());
            }
            return 0;
        }
        public static String getStringQuery(string id)
        {

            if (HttpContext.Current.Request.QueryString[id] != null)
            {

                return HttpContext.Current.Request.QueryString[id].ToString();
            }
            return "";
        }
        public static String getStringFrom(string id)
        {

            if (HttpContext.Current.Request.Form[id] != null)
            {

                return HttpContext.Current.Request.Form[id].ToString();
            }
            return "";
        }
        public static int getIntFrom(string id)
        {
            int number;
            if (HttpContext.Current.Request.Form[id] != null)
            {
                if ((!Int32.TryParse(HttpContext.Current.Request.Form[id].ToString(), out number)))
                {
                    return 0;
                }
                return int.Parse(HttpContext.Current.Request.Form[id].ToString());
            }
            return 0;
        }
        public static String getStringSession(String txt)
        {
            if (HttpContext.Current.Session[txt] != null)
            {
                return HttpContext.Current.Session[txt].ToString();
            }
            return "";
        }
        public static int getIntSession(String txt)
        {
            int number;
            if (HttpContext.Current.Session[txt] != null)
            {
                if ((!Int32.TryParse(HttpContext.Current.Session[txt].ToString(), out number)))
                {
                    return 0;
                }
                return Int32.Parse(HttpContext.Current.Session[txt].ToString());
            }
            return 0;
        }

        public static string getMD5(string txt)
        {
            string source = txt;
            MD5 md5 = MD5.Create();
            string hash = GetMd5Hash(md5, source);
            return hash;
        }

        static string GetMd5Hash(MD5 md5Hash, string input)
        {

            // Convert the input string to a byte array and compute the hash. 
            byte[] data = md5Hash.ComputeHash(Encoding.UTF8.GetBytes(input));

            // Create a new Stringbuilder to collect the bytes 
            // and create a string.
            StringBuilder sBuilder = new StringBuilder();

            // Loop through each byte of the hashed data  
            // and format each one as a hexadecimal string. 
            for (int i = 0; i < data.Length; i++)
            {
                sBuilder.Append(data[i].ToString("x2"));
            }

            // Return the hexadecimal string. 
            return sBuilder.ToString();
        }

        // Verify a hash against a string. 
        static bool VerifyMd5Hash(MD5 md5Hash, string input, string hash)
        {
            // Hash the input. 
            string hashOfInput = GetMd5Hash(md5Hash, input);

            // Create a StringComparer an compare the hashes.
            StringComparer comparer = StringComparer.OrdinalIgnoreCase;

            if (0 == comparer.Compare(hashOfInput, hash))
            {
                return true;
            }
            else
            {
                return false;
            }
        }




    }

}
